package com.huarun.common.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * @author hqy
 * @创建时间 2018-11-19 16:52
 * @Description 创建token
 */
public class TokenUtil {
	/**
	 * 生成运营管理平台token
	 * 
	 * @param request
	 * @param params参数
	 * @return
	 */
	public static String createToken(String url, HttpServletRequest request, Long time, Map<String, Object> params) {
		String secret = Constant.MANAGE_SECRET;
		String appkey = Constant.MANAGE_APPKEY;
		Object[] keys = null;
		if (null != params) {
			keys = params.keySet().toArray();
			Arrays.sort(keys);
		}
		StringBuilder sb = new StringBuilder();
		sb.append(url);
		String currentMethod = Thread.currentThread().getStackTrace()[1].getMethodName();

		// String method = request.getMethod().toLowerCase();
		String method = "get";
		if (Constant.METHOD_GET.equals(method)) {
			if (null != keys) {
				for (Object key : keys) {
					Object value = params.get(key);
					sb.append(key);
					sb.append("=");
					sb.append(String.valueOf(value));
					sb.append("&");
				}
			}
			sb.append("appkey=").append(appkey).append("&time=").append(time).append(secret);
		} else {
			sb.append("{");
			if (null != keys) {
				for (Object key : keys) {
					Object value = params.get(key);
					sb.append(key);
					sb.append(":");
					sb.append(String.valueOf(value));
					sb.append(",");
				}
			}
			sb.append("appkey:").append(appkey).append(",time:").append(time).append("}").append(secret);
		}
		System.out.println("生成token的字符串------------------------------------------" + sb.toString());
		String md5 = MD5Util.getMD5(sb.toString(), Constant.CHARSET);
		return md5.toUpperCase();
	}

	public static Map<String, Object> createToken(String url, Long time, Map<String, Object> params) {
		Map<String, Object> map = new HashMap<String, Object>();
		String secret = Constant.MANAGE_SECRET;
		String appkey = Constant.MANAGE_APPKEY;
		Object[] keys = null;
		if (null != params) {
			keys = params.keySet().toArray();
			Arrays.sort(keys);
		}
		StringBuilder sb = new StringBuilder();
		sb.append(url);

		//此处多加了parms 是为了解决 加密前的数据和传递给戎光的数据不一致的问题 update by eva 2019/3/29
		StringBuilder parms = new StringBuilder();
		parms.append("{");
//		sb.append("{");
		if (null != keys) {
			for (Object key : keys) {
				Object value = params.get(key);
				StringBuilder tempParm = new StringBuilder();
				if ("FileContent".equals(key) || "UseTimeInfo".equals(key) || "RoomInfo".equals(key)
						|| "ChildInfo".equals(key)) {

					tempParm.append("\"" + key + "\"");
					tempParm.append(":");
					tempParm.append(String.valueOf(value));
					tempParm.append(",");
					parms.append(tempParm);
				} else {
					tempParm.append("\"" + key + "\"");
					tempParm.append(":");
					tempParm.append("\"" + String.valueOf(value) + "\"");
					tempParm.append(",");
					parms.append(tempParm);
				}
			}
			sb.append(parms);
		}
		parms.append("\"appkey\":").append("\"" + appkey + "\"").append(",\"time\":").append(time).append("}");
		sb.append("\"appkey\":").append("\"" + appkey + "\"").append(",\"time\":").append(time).append("}")
				.append(secret);
		System.out.println("生成token的字符串------------------------------------------" + sb.toString());
		System.out.println("传递给戎光的字符串------------------------------------------" + parms.toString());
		String md5 = MD5Util.getMD5(sb.toString(), Constant.CHARSET);
		System.out.println("生成的token:" + md5.toUpperCase() + "------appkey:" + appkey);
		map.put("parms", parms);
		map.put("token", md5.toUpperCase());
		return map;
	}
}
